import type { FieldRule } from "vant";
import type { VNode } from "vue"
export * from './type_group'

export type LOGINTYPE = '100' | '101' | '102'
export type dateType = 'year' | 'month' | 'date' | 'datetime' | 'week' | 'datetimerange' | 'daterange' | 'monthrange' | 'yearrange'

export interface listButton<T> {
    text: string
    icon?: string
    type?: 'primary' | 'success' | 'warning' | 'danger'
    disabled?: boolean
    className?: string
    hasConfirm?: boolean
    confirmText?: string
    onClick: (data: T, index: number) => void
}
export interface selectItem {
    label: string
    value: string | number
    children?: selectItem[]
    [key: string]: any
}
export interface interJson {
    type: 'json'
}

interface interSelect<T> {
    type: 'select',
    options?: selectItem[]
    selectName?: keyof T
    multiple?: boolean
    inputable?: boolean
    props?: {
        label?: string
        value?: string
        children?: string
    }
    link?: true
}
interface interCascader<T> {
    type: 'cascader',
    options?: selectItem[]
    selectName?: keyof T
    multiple?: boolean
    checkStrictly?: boolean
    props?: {
        label?: string
        value?: string
        children?: string
    }
    link?: true
}

interface interRadio<T, U> {
    type: 'radio' | 'checkbox',
    options: selectItem[]
    selectName?: U
    onChange?: ({ prop, value, data, index }: { prop: keyof T, value: boolean | number | string, data: T, index: number }) => Promise<void>
}

interface interBool {
    type: 'bool',
    trueText?: string
    falseText?: string
}

interface interSwitch<T extends Record<string, any>> {
    type: 'switch',
    loading?: boolean,
    onChange: ({ prop, data, value, index }: { prop: keyof T, value: boolean | number | string, data: T, index: number }) => Promise<void>
}

interface interNumber {
    type: 'number'
    min?: number
    max?: number
}
interface interImage {
    type: 'image' | 'images',
    size?: string | number
    formSize?: string | number
    maxShow?: number
    after?: (url: string) => string
}
interface interText {
    type: 'text'
    base?: string
    keyEnter?: () => void
}
interface interfaceDate {
    type: 'date'
    format?: string
    dateType?: dateType
}
interface interTextarea {
    type: 'textarea'
    maxlength?: number
    rows?: number
    keyEnter?: () => void
}
interface interPassword {
    type: 'password'
    keyEnter?: () => void
}
export interface interCustomize<T extends Record<string, any>, U extends keyof T & string> {
    type: 'customize',
    vform: ({ prop, data, value }: { prop: U, data: T, value: T[U] }) => VNode
    p_vform?: ({ prop, data, value }: { prop: U, data: T, value: T[U] }) => VNode
    formatter?: ({ prop, data, value }: { prop: U, data: T, value: T[U] }) => VNode | string | undefined
    p_formatter?: ({ prop, data, value }: { prop: U, data: T, value: T[U] }) => VNode | string | undefined
    link?: boolean
}
export interface interCustomizeTable<T extends Record<string, any>, U extends keyof T & string> {
    type: 'customize',
    formatter: ({ prop, data, value, index }: { prop: U, data: T, value: T[U], index: number }) => VNode | string | undefined
    p_formatter?: ({ prop, data, value, index }: { prop: U, data: T, value: T[U], index: number }) => VNode | string | undefined
    link?: boolean
}
interface interHtml<T extends Record<string, any>, U extends keyof T & string> {
    type: 'html'
    hformatter: ({ prop, data, value }: { prop: U, data: T, value: T[U] }) => string | undefined
}
export interface interFormBase<T extends Record<string, any>, U extends keyof T & string> {
    prop: string | U
    label: string
    formWidth?: string | number
    show?: ({ data, type, isPhone }: { data: T, type: 'form' | 'detail' | 'search' | 'list', isPhone: boolean }) => boolean
    onChange?: ({ value, data, item }: { value: T[U], data: T, item?: any }) => void
    inpDisabled?: boolean
    required?: boolean
    rule?: FieldRule[]
    placeholder?: string
    overflow?: boolean
    label_width?: string | number
    clearable?: boolean
    inpDefault?: (data: T) => string | number | boolean
    default?: string | VNode
    sortBy?: number
    link?: boolean
}
export type interForm<T extends Record<string, any>, U extends keyof T & string> = interFormBase<T, U>
    & (interCustomize<T, U> | interHtml<T, U> | interSelect<T> | interCascader<T> | interRadio<T, U> | interBool | interNumber | interImage | interText | interTextarea | interPassword | interfaceDate)


export interface interTableBase<T, U extends keyof T & string> {
    prop: string | U
    label: string
    width?: string | number
    default?: string | VNode
    sortable?: boolean
    overflow?: boolean
    labelWidth?: string
    sortBy?: number
    show?: ({ data, type, isPhone }: { data?: T, type: 'form' | 'detail' | 'search' | 'list', isPhone: boolean }) => boolean
}
export type interTable<T extends Record<string, any>, U extends keyof T & string> = interTableBase<T, U>
    & (interRadio<T, U> | interSelect<keyof T> | interImage | interText | interBool | interSwitch<T> | interCustomizeTable<T, U> | interJson)

export interface interRule {
    [key: string]: {
        rule: 'isRequired' | 'isLength' | 'isRegExp' | 'isNotRegExp' | 'isNumbersize' | 'isIdcard' | 'isPhone', [key: string]: string
    }[]
}